﻿using Microsoft.Win32;
using Sunny.UI;
using sync_signal_light.subs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sync_signal_light
{
    public partial class FrmSetting : Sunny.UI.UIForm
    {
        public FrmSetting()
        {
            InitializeComponent();
        }

        private void FrmSetting_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "参数配置 - " + Application.ProductVersion;
                Addrs.Text = String.Join(", ", subs.opini.CLS_CFG.addrs);
                loading_serial_info();
                PORT_LIGHT.Text = subs.opini.CLS_CFG.light_port.ToString();
                PORT_ALARM.Text = subs.opini.CLS_CFG.alarm_port.ToString();
                tbx_sync_delay.Text = subs.opini.CLS_CFG.sync_delay.ToString();
                chk_sync.Checked = subs.opini.CLS_CFG.sync_enable;
                chk_start.Checked = subs.opini.CLS_CFG.start_enable;
                tbx_light_delay.Text = opini.CLS_CFG.light_delay.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void loading_serial_info()
        {
            string strRegPath = @"Hardware\\DeviceMap\\SerialComm";//定义注册表子Path
            RegistryKey regRootKey;//创建两个RegistryKey类，一个将指向Root Path，另一个将指向子Path
            RegistryKey regSubKey;
            regRootKey = Registry.LocalMachine;
            regSubKey = regRootKey.OpenSubKey(strRegPath);
            string[] strCommList = regSubKey.GetValueNames();
            if (strCommList.Length > 0)
            {
                PORT_LIGHT.Items.Clear();
                PORT_ALARM.Items.Clear();
                foreach (string VName in strCommList)
                {
                    //向listbox1中添加字符串的名称和数据，数据是从rk对象中的GetValue(it)方法中得来的
                    PORT_LIGHT.Items.Add(regSubKey.GetValue(VName));
                    PORT_ALARM.Items.Add(regSubKey.GetValue(VName));
                }
            }
            else
            {
                MessageBox.Show("当前计算机没有可用串口！");
            }
            //关闭
            regSubKey.Close();
            regRootKey.Close();

        }
        private void btn_save_Click(object sender, EventArgs e)
        {
            Addrs.Text.Replace("，",",");
            opini.CLS_CFG.addrs = Addrs.Text.Split(new char[] { ',', '，', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            opini.CLS_CFG.light_port = PORT_LIGHT.Text;
            opini.CLS_CFG.alarm_port = PORT_ALARM.Text;
            opini.CLS_CFG.sync_delay = int.Parse(tbx_sync_delay.Text);
            opini.CLS_CFG.sync_enable = chk_sync.Checked;
            opini.CLS_CFG.start_enable = chk_start.Checked;
            opini.CLS_CFG.light_delay = int.Parse(tbx_light_delay.Text);
            opini.write_config("system", "configs", Json.Serialize(opini.CLS_CFG));
            opini.CLS_PARA.update = true;//通知参数更新
            this.Close();
        }
    }
}
